package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/8/16.<br><br>
 *
 * 中等：Z 字形变换
 */
public class Topic6 {

    public static void main(String[] args) {
        Topic6 instance = new Topic6();

        System.out.println(instance.convert("PAYPALISHIRING", 3));
        System.out.println(instance.convert("PAYPALISHIRING", 4));
        System.out.println(instance.convert("A", 1));
    }

    public String convert(String s, int numRows) {
        char[] array = s.toCharArray();
        StringBuilder[] sbArray = new StringBuilder[numRows];

        for(int i = 0; i < numRows; i++) {
            sbArray[i] = new StringBuilder();
        }

        int i = 0;
        while (i < array.length) {
            for(int sbIndex = 0; sbIndex < numRows && i < array.length; sbIndex++) {
                sbArray[sbIndex].append(array[i++]);
            }

            for(int sbIndex = numRows - 2; sbIndex >= 1 && i < array.length; sbIndex--) {
                sbArray[sbIndex].append(array[i++]);
            }
        }

        for(i = 1; i < numRows; i++) {
            sbArray[0].append(sbArray[i]);
        }

        return sbArray[0].toString();
    }
}
